using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Mail;

namespace Common.Email
{
    public class SmtpSender
    {
        string _host;
        int _port;
        string _from;
        string _user;
        string _pwd;
        bool _enableSsl;
        Encoding _encoding = Encoding.Default;

        public SmtpSender(string host, string from)
            : this(host, "","", from)
        {
        }

        public SmtpSender(string host,string user,string pwd, string from)
            : this(host, 25,user,pwd,false, from,Encoding.Default)
        {
        }

        public SmtpSender(string host, string user, string pwd, bool enableSsl, string from)
            : this(host, 25, user, pwd, enableSsl, from, Encoding.Default)
        {
        }

        public SmtpSender(string host, int port, string user, string pwd, bool enableSsl, string from, Encoding encoding)
        {
            _host = host;
            _port = port;
            _user = user;
            _pwd = pwd;
            _enableSsl = enableSsl;
            _from = from;
            _encoding = encoding;
        }

        public void Send(string to,string subject,string body)
        {
            SmtpClient mailClient = new SmtpClient(_host,_port);
            MailMessage mail = new MailMessage();
            try
            {
                mail.From = new MailAddress(_from);
                mail.To.Add(to);
                mail.Subject = subject;
                mail.Body = body;
                mail.IsBodyHtml = true;
                if (_enableSsl)
                {
                    mailClient.EnableSsl = _enableSsl;
                }
                if (_encoding != mail.BodyEncoding)
                {
                    mail.BodyEncoding = _encoding;
                    mail.SubjectEncoding = _encoding;
                }
                if (!string.IsNullOrEmpty(_user))
                {
                    mailClient.Credentials = new NetworkCredential(_user, _pwd);
                }
                else
                {
                    mailClient.UseDefaultCredentials = true;
                }
                mailClient.Send(mail);
            }
            catch
            {
                try
                {
                    mailClient.SendAsync(mail, null);
                }
                catch
                {
                    try
                    {
                        mailClient.Send(mail);
                    }
                    catch (FormatException ex)
                    {
                        throw ex;
                    }
                    catch (SmtpException ex)
                    {
                        throw ex;
                    }
                    catch (Exception e)
                    {
                        throw e;
                    }
                }
            }
            finally
            {
                mail.Dispose();
            }
        }
    }
}
